package blog.webcontroller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import blog.domain.Article;
import blog.domain.Comment;
import blog.domain.User;
import blog.service.ArticleService;
import blog.service.UserService;


@Controller
public class ArticleController {
	
	@Resource
	private ArticleService articleService;
	
	@Resource
	private UserService userSerivce;

	@RequestMapping("/")
	public String redirectRoot(HttpSession session) 
	{
		Object user = session.getAttribute("user");
		if(user == null) {
			articleService.initEditable();
		}
		return "redirect:/articles";
	}
	
	@RequestMapping(value="/articles", method=RequestMethod.GET)
	public String getAll(Model model) 
	{
		model.addAttribute("articles", articleService.getArticles());
		model.addAttribute("topArticles", articleService.getTopTenArticle());
		model.addAttribute("topComments", articleService.getTopTenComment());
		return "articleList";
	}
	
	@RequestMapping(value="/articles/add", method=RequestMethod.GET)
	public String showAddArticle(Model model) {
		model.addAttribute("topArticles", articleService.getTopTenArticle());
		return "addArticle";
	}
	
	@RequestMapping(value="/articles", method=RequestMethod.POST)
	public String add(Article article, @RequestParam String name)
	{
		User user = userSerivce.getUser(name);
		article.setUser(user);
		articleService.addArticle(article);
		
		return "redirect:/articles";
	}
	
	@RequestMapping(value="/articles/addcomment/{id}", method=RequestMethod.POST)
	public String addComment(@PathVariable int id, @RequestParam String content, Model model)
	{
		Article article = articleService.getArticle(id);
		Comment comment = new Comment(content, new Date());
		article.addComment(comment);
		comment.setArticle(article);
		articleService.updateArticle(article);
		model.addAttribute("article", article);
		return "articleDisplay";
	}
	
	@RequestMapping(value="/articles/{id}", method=RequestMethod.GET)
	public String get(@PathVariable int id, Model model) {
		model.addAttribute("article", articleService.getArticle(id));
		model.addAttribute("topArticles", articleService.getTopTenArticle());
		return "articleDisplay";
	}
	
	@RequestMapping(value="/articles/{id}", method=RequestMethod.POST)
	public String update(@PathVariable int id, @RequestParam String plcontent, Model model)
	{
		Article article = articleService.getArticle(id);
		article.setContent(plcontent);
		articleService.updateArticle(article);
		model.addAttribute("article", article);
		model.addAttribute("topArticles", articleService.getTopTenArticle());
		return "articleDisplay";
	}
	
	@RequestMapping(value="/articles/delete/{id}", method=RequestMethod.POST)
	public String delete(@PathVariable  int id) {
		Article article = articleService.getArticle(id);
		
		articleService.deleteArticle(article);
		return "redirect:/articles";
	}
}